package com.tanhua.app.controller;

import com.aliyuncs.exceptions.ClientException;
import com.tanhua.app.service.BigDataService;
import com.tanhua.app.service.VideoService;
import com.tanhua.domain.UserHolder;
import com.tanhua.domain.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private BigDataService bigDataService;

    @GetMapping(value = "/smallVideos", name = "查询小视频")
    public ResponseEntity smallVideos(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer pagesize) throws ClientException, IOException {
        if(page==0){
            page=1;
        }
        PageBeanVo pageBeanVo = videoService.smallVideos(page, pagesize);
        return ResponseEntity.ok(pageBeanVo);
    }

    @PostMapping(value = "/smallVideos", name = "小视频的上传")
    public ResponseEntity uploadVideo(MultipartFile videoThumbnail,MultipartFile videoFile) throws ClientException, IOException {

        bigDataService.toMqByVideo(5,null);
        return videoService.uploadVideo(videoThumbnail,videoFile);
    }


    @PostMapping(value = "/smallVideos/{uid}/userFocus", name = "小视频作者的关注")
    public ResponseEntity userFocus(@PathVariable("uid") Long uid) throws ClientException, IOException {

        return videoService.userFocus(uid, UserHolder.findUser().getId());
    }


    @PostMapping(value = "/smallVideos/{uid}/userUnFocus", name = "小视频作者的关注撤销")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long uid) throws ClientException, IOException {

        return videoService.userUnFocus(uid, UserHolder.findUser().getId());
    }
}
